import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';

import { Meteor } from 'meteor/meteor';

class FamiliesCollection extends Mongo.Collection {
    update(selector, modifier) {
        const result = super.update(selector, modifier);
        return result;
    }
    insert(family, callback) {
        const ourFamilies = family;
        const result = super.insert(ourFamilies, callback);
        return result;
    }
    remove(selector, callback) {
        const result = super.remove(selector, callback);
        return result;
    }
}

export const Families = new FamiliesCollection('families');

Families.deny({
    insert() { return true; },
    update() { return true; },
    remove() { return true; },
});

Families.schema = new SimpleSchema({
    name: {
        label: '家庭名称',
        type: String
    },
    logo: {
        label: '家庭图片',
        type: String,
        optional: true
    },
    address: {
        label: '家庭地址',
        type: String,
        optional: true
    },
    rooms: {
        label: '房间',
        type: [Object],
        optional: true
    },
    'rooms.$._id': {
        type: String,
        label: '房间编号',
        optional: true
    },
    'rooms.$.name': {
        type: String,
        label: '房间名称',
        optional: true
    },
    'rooms.$.url': {
        type: String,
        label: '图片路径',
        optional: true
    },
    'rooms.$.isDef': {
        type: Boolean,
        label: '是否为默认房间',
        optional: true
    },
    desc: {
        label: '说明',
        type: String,
        optional: true
    },
    belongTo: {
        label: '拥有者',
        type: [Object],
        optional: true
    },
    'belongTo._id': {
        label: '用户ID',
        type: String
    },
    'belongTo.name': {
        label: '用户名称',
        type: String
    },
    createdUser: {
        type: Object, 
        label: '创建人'
    },
    'createdUser._id':{
        type: String,
        optional: true,
        label: '创建人ID'
    },
    'createdUser.name':{
        type: String,
        optional: true,
        label: '创建人名称'
    },
    isDefault: {
        label: '是否为默认家庭',
        type: Boolean,
        optional: true
    },
    createdAt: {
        type: Date,
        denyUpdate: true,
        optional: true,
        autoValue() {
            if (this.isInsert) {
                return new Date();
            } else if (this.isUpsert) {
                return {$setOnInsert: new Date()};
            }
            return this.unset();
        }
    }
});
Families.attachSchema(Families.schema);

/*
var http = require('http');
http.createServer(function (request, response) {
// 发送 HTTP 头部
// HTTP 状态值: 200 : OK
// 内容类型:
// text/plain
// response.writeHead(200, {'Content-Type': 'text/plain'}); 	// 发送响应数据 "Hello World" 	response.end('Hello World\n'); }).listen(80);
*/
